package org.example.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//直接将限流和降级定义在方法中-自定义拦截
@RestController
@Slf4j
public class AnnoController {
    @RequestMapping(value = "/anno1")
    @SentinelResource(value = "anno1",
            blockHandler = "anno1BlockHandler", //当前方法如果被限流或者降级会调用这个方法
            fallback = "anno1Fallback" //当前方法如果异常
    )
    public String anno1(String name){
        if ("wolfcode".equals(name)){
            throw new RuntimeException();
        }
        return "anno1";
    }

    public String anno1BlockHandler(String name, BlockException e){
        log.error("{}",e);
        return "接口被限流或者降级了";
    }
    //throwable时进入的方法
    public String anno1Fallback(String name,Throwable throwable){
        log.error("{}",throwable);
        return "接口发生异常了";
    }
}
